//
//  KeChengXueShengView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/17.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "KeChengXueShengView.h"
#import "SYRequestAPI+Teach.h"
#import "JiaoXueKeChengWeiShangKeShiModel.h"

@implementation KeChengXueShengView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithClassID:(NSString *)classID andLessonID:(NSString *)lessonID WithFrame:(CGRect)rect{
    if ([super initWithFrame:rect]) {
        _dataArray = [NSMutableArray new];
        self.classID = classID;
        self.lessonId = lessonID;
        [self initSubView];
        [self getKeChengXueShengLisetRequest];
    }
    return self;
}

- (void)initSubView{
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@-84);
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[KeChengNextCell class] forCellReuseIdentifier:@"KeChengNextCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _headerView = [[KeChengNextHeaderView alloc]initWithClassID:self.classID andLessonID:self.lessonId andFrame:CGRectMake(0, 0,self.frame.size.width, 131)];
        _headerView.frame = CGRectMake(0, 0,self.frame.size.width, 131);
        tableView.tableHeaderView = _headerView;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KeChengNextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeChengNextCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[KeChengNextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KeChengNextCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    cell.index = (int)indexPath.row;
    
    UILabel *labelXueHaoLabel = (UILabel *)[cell viewWithTag:20];//学号
    UILabel *labelNameLabel = (UILabel *)[cell viewWithTag:21];//姓名
    UILabel *labelGenderLabel = (UILabel *)[cell viewWithTag:22];//性别
    UILabel *labelLinker = (UILabel *)[cell viewWithTag:23];//联系人
    UILabel *labelScoreLabel = (UILabel *)[cell viewWithTag:24];//电话
    UILabel *labeStatus = (UILabel *)[cell viewWithTag:25];//状态
    UILabel *labelKaoQing = (UILabel *)[cell viewWithTag:26];//考勤
    JiaoXueKeChengKeChengXueShengModel *model = _dataArray[indexPath.row];
    
    labelXueHaoLabel.text = model.serial;
    labelNameLabel.text = model.name;
    if ([model.sex isEqualToString:@"1"]) {
        labelGenderLabel.text = @"男";
    }else if ([model.sex isEqualToString:@"2"]){
        labelGenderLabel.text = @"女";
    }else{
        labelGenderLabel.text = @"未知";
    }
    labelLinker.text = model.linker;
    labelScoreLabel.text = model.mobile;
    labeStatus.text = model.statusName;
    if ([model.status isEqualToString:@"0"]) {
        //无效
        labelKaoQing.text = @"请假";
    }else{
        //有效
        labelKaoQing.text = @"正常";
    }
    cell.button.tag = indexPath.row+100;
   [cell.button addTarget:self action:@selector(lookUp:) forControlEvents:UIControlEventTouchUpInside];
    cell.mainW = _mainw;
    [cell reloadViewLayout];
    return cell;
}
- (void)lookUp:(UIButton *)sender{
    int num = (int)sender.tag-100;
    
//    id contModel = _dataArray[num];
    
    
    
    
   JiaoXueKeChengKeChengXueShengModel *model = _dataArray[num];
//
//    
//
// 。JiaoXueKeChengWeiShangKeShiModel http://apipad.31waiyu.com/common/CommonService.svc/LessonStudent
    if ([self.myDelagete respondsToSelector:@selector(addChildViewForDetailAndID:)]) {
        [self.myDelagete addChildViewForDetailAndID:model.studentId];
    }
    
    //NSLog(@"当前的NUM %d  当前视图状态 %@",num,self.myDelagete);
//    NSAssert(self.myDelagete = nil, @"property mydelegate is nil");
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[SJXQHeaderTitleView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44) andArray:@[@"学号",@"姓名",@"性别",@"联系人",@"电话",@"状态",@"考勤",@"详情"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    _titleView.frame = CGRectMake(0, 0, self.frame.size.width, 44);
    return _titleView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
#pragma mark - 得到W未上课时的课程学生
- (void)getKeChengXueShengLisetRequest{
    NSDictionary *parama = @{@"lessonId":_lessonId,
                             @"serial":@"",
                             @"name":@"",
                             @"status":@"",
                             @"tryStatus":@""};
//    [MBProgressHUD mj_showError:nil toView:self];
//    [];
    [SYRequestAPI sy_getClassStudent:parama success:^(id  _Nonnull responseObject){
      //  NSLog(@"教学-课程-未上课时-课程学生 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        for (int i = 0; i<array.count; i++) {
            
            JiaoXueKeChengKeChengXueShengModel *model = [JiaoXueKeChengKeChengXueShengModel
                                                       yy_modelWithJSON:array[i]];
            [self->_dataArray addObject:model];
        }
        [self.tableList reloadData];
        [MBProgressHUD mj_hideHUDForView:self];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
    }];
    
}
//- (void)getClassDetail{
//    _headerView.classID = self.classID;
//    _headerView.lessonId = self.lessonId;
//    [_headerView getCourseDetailRequest];
//}

//#pragma mark - 得到课程详情数据
//- (void)getKeChengXueShengRequest{
//    NSDictionary *parama = @{@"classId":_classID};
//    [MBProgressHUD mj_showLoading];
//    [SYRequestAPI sy_getLessonDetail:parama success:^(id  _Nonnull responseObject) {
//        NSLog(@"教学-课程-课程详情 %@",responseObject);
//        self->_classDetailModel = [responseObject objectForKey:@"data"];
//        [MBProgressHUD mj_hideHUD];
//    } fail:^(NSError *error) {
//        [MBProgressHUD mj_hideHUD];
//    }];
//}

@end
